<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<title>My JSP 'putInStorage.jsp' starting page</title>
		<script type="text/javascript">
	var index = 0;

	document.onkeydown = function(e) {
		if (index == 0) {
			return;
		}
		if (!e)
			e = window.event;//火狐中是 window.event
		if ((e.keyCode || e.which) == 13) {
			index = 0;
			ajaxPutInStorage();
		}
	};

	function ajaxGetBoxMessage() {
		index = 0;
		var tempString = "";
		$
				.ajax({
					type : 'post',
					url : '${ctx}/boxinStorage/ajaxGetBoxMessage',
					data : 'boxCode=' + $("#boxCode").val(),
					dataType : 'json',
					success : function(data) {

						if (data.status == "1") {
							index = 1;
							tempString += "<div>所属批次:" + data.content.batchCode
									+ "</div>";
							tempString += "<div>商家:"
									+ data.customers.customerName + "</div>";
							tempString += "<div>箱子类型:" + data.content.boxType
									+ "</div>";
							tempString += "<div>箱子商品数量:" + data.content.goods_num
									+ "</div>";
							tempString += "请输入箱子商品数量：<input type=\"text\" id=\"checkId\" onchange=\"ajaxPutInStorage(this);\">";
						} else if (data.status == "2") {
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val()
									+ ":箱子不属于该服务网点!</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();
						} else if (data.status == "0") {
							tempString += "<span class=\"alert alert-block\">服务器异常!</span>";
						} else if (data.status == "-1") {
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val() + ":箱子已经入库!</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();

						} else {
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val() + "：" + data.content
									+ "</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();

						}
						$("#boxMessage").html(tempString);
						$("#checkId").focus();
					},

					error : function() {
						tempString += "<span class=\"alert alert-block\">服务器异常!</span>";
						$("#boxMessage").html(tempString);
					}
				});
	}

	function ajaxPutInStorage(item) {
		var tempBoxCode = $("#boxCode").val();
		
		if (tempBoxCode == "") {
			return;
		}
		var tempString = "";
		$
				.ajax({
					type : 'post',
					url : '${ctx}/boxinStorage/ajaxPutInStorage',
					data : 'boxCode=' + tempBoxCode + '&storageId='
							+ $("#storages").val()+'&skuNumber='+item.value,
					dataType : 'json',
					success : function(data) {

						if (data.status == "1") {

							$("#message").html(
									"<button data-dismiss=\"alert\" class=\"close\">×</button>"
											+ $("#boxCode").val() + "入库成功");
							$("#message").attr("class", "alert alert-success");
							tempString += "<tr><td>" + data.content.batchCode
									+ "</td>" + "<td>" + $("#boxCode").val()
									+ "</td><td>" + data.content.boxType
									+ "</td><td>" + data.realname + "</td><td>"
									+ data.content.customers.customerName
									+ "</td><td>入库</td><td>"+data.content.goodsStatus+"</td></tr>";
							$("#tbodyBox").append(tempString);
							$("#boxCode").val("");
							$("#boxCode").focus();
							$("#boxMessage").html("");
							setTimeout(function() {
								$("#message").removeClass();
								$("#message").html("");
							}, 6000);
						} else if (data.status == "2") {
							$("#message").html(
									"<button data-dismiss=\"alert\" class=\"close\">×</button>"
											+ $("#boxCode").val() + "入库失败");
							$("#message").attr("class", "alert alert-block");
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val()
									+ ":箱子不属于该服务网点!</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();
							$("#boxMessage").html(tempString);
						} else if (data.status == "0") {
							tempString += "<span class=\"alert alert-block\">服务器异常!</span>";
							$("#boxMessage").html(tempString);
							$("#message").html(
									"<button data-dismiss=\"alert\" class=\"close\">×</button>"
											+ $("#boxCode").val() + "入库失败");
							$("#message").attr("class", "alert alert-block");
						} else if (data.status == "-1") {
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val() + ":箱子已经入库!</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();
							$("#boxMessage").html(tempString);
							$("#message").html(
									"<button data-dismiss=\"alert\" class=\"close\">×</button>"
											+ $("#boxCode").val() + "入库失败");
							$("#message").attr("class", "alert alert-block");
						} else if (data.status == "-2") {
							tempString += "<span class=\"alert alert-block\">"
									+ $("#boxCode").val() + ":" + data.content
									+ "</span>";
							$("#boxCode").val("");
							$("#boxCode").focus();
							$("#boxMessage").html(tempString);
							$("#message").html(
									"<button data-dismiss=\"alert\" class=\"close\">×</button>"
											+ $("#boxCode").val() + "入库失败");
							$("#message").attr("class", "alert alert-block");
						}

					},

					error : function() {
						tempString += "<span class=\"alert alert-block\">服务器异常!</span>";
						$("#boxMessage").html(tempString);
						$("#message").html(
								"<button data-dismiss=\"alert\" class=\"close\">×</button>"
										+ $("#boxCode").val() + "入库失败");
						$("#message").attr("class", "alert alert-block");
					}
				});
	}
</script>
	</head>

	<body>
		<div id="message">

		</div>
		<div class="control-group">
			<label for="pmsprinters_corpLevel" class="control-label">
				选择入库片区:
			</label>
			<div class="controls">
				<select id="storages" name="storageId"
					onchange="companyAjaxExpressStyle()">
					<c:forEach items="${storages}" var="storage">
						<option value="${storage.id}">
							${storage.name}
						</option>
					</c:forEach>
				</select>
			</div>
		</div>
		<div class="control-group">
			<label for="pmsprinters_corpLevel" class="control-label">
				箱子条码:
			</label>
			<div class="controls">
				<input type="text" id="boxCode" onchange="ajaxGetBoxMessage()" />
			</div>
		</div>
		<div id="boxMessage">

		</div>

		<table id="contentTable"
			class="table table-striped table-bordered table-condensed">
			<thead>
				<tr>

					<th>
						批次
					</th>
					<th>
						箱子
					</th>
					<th>
						类型
					</th>
					<th>
						操作人
					</th>
					<th>
						商家
					</th>
					<th>
						状态
					</th>
					<th>
						商品数量差异
					</th>
				</tr>
			</thead>
			<tbody id="tbodyBox">

			</tbody>
		</table>
	</body>
</html>
